package cn.hom1.utils;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.websocket.RemoteEndpoint.Basic;
import javax.websocket.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 描述
 *
 * @author iksen
 * @date 2019-01-22 15:04
 */
public class WebSocketUtils {

  private static final Logger logger = LoggerFactory.getLogger(WebSocketUtils.class);

  // 存储 websocket session
  public static final Map<String, Session> ONLINE_USER_SESSIONS = new ConcurrentHashMap<>();

  public static void sendMessage(Session session, String message) {
    if (session == null) {
      return;
    }
    Basic basic = session.getBasicRemote();
    if (basic == null) {
      return;
    }
    try {
      basic.sendText(message);
    } catch (IOException e) {
      logger.error("sendMessage IOException ",e);
    }
  }

  public static void sendMessageAll(String message) {
    ONLINE_USER_SESSIONS.forEach((sessionId, session) -> sendMessage(session, message));
  }
}
